// 114.二叉树展开为链表
package Leetcode_Second_Brush;

public class Solution114 {
    public void flatten(TreeNode root) {
        while (root != null) {
            if (root.left != null) {
                // 获取左子树
                TreeNode leftSubtree = root.left;
                // 获取左子树的最右边的节点
                TreeNode rightmost = leftSubtree;
                while (rightmost.right != null) {
                    rightmost = rightmost.right;
                }
                // 将原来的右子树拼接到该节点上
                rightmost.right = root.right;
                // 调整 root 的左右子树
                root.right = root.left;
                root.left = null;
            }
            root = root.right;
        }
    }
}
